package com.example.controller;

import com.example.constant.SysJobConstant;
import com.example.enums.SysHandlerEnum;
import com.example.po.SysJob;
import com.example.service.SysJobService;
import com.example.utils.IDUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class SysJobController {

    @Resource
    private SysJobService sysJobService;

    /***
     * 发送MQ消息
     * @return
     */
    @RequestMapping(value = "sendMQ")
    public Long sendMQ() {
        String id = IDUtils.getId();
        SysJob sysJob = new SysJob();
        sysJob.setStatus(SysJobConstant.ING);
        sysJob.setType(SysHandlerEnum.MQSendHandler.getCode());
        sysJob.setTarget(id);
        sysJob.setContext(id);
        sysJob.setCreateTime(new Date());
        sysJobService.save(sysJob);
        return sysJob.getId();
    }

    /***
     * 发送SMS消息
     * @return
     */
    @RequestMapping(value = "sendSMS")
    public Long sendSMS() {
        String id = IDUtils.getId();
        SysJob sysJob = new SysJob();
        sysJob.setStatus(SysJobConstant.ING);
        sysJob.setType(SysHandlerEnum.SMSSendHandler.getCode());
        sysJob.setTarget(id);
        sysJob.setContext(id);
        sysJob.setCreateTime(new Date());
        sysJobService.save(sysJob);
        return sysJob.getId();
    }

    /****
     * 批量发送MQ消息
     * @return
     */
    @RequestMapping(value = "sendBatchSMS")
    public Integer sendBatchSMS(@RequestParam(defaultValue = "600") int maxSize) throws Exception {
        List<SysJob> sysJobList = new ArrayList<>();
        for (int i = 0; i < maxSize; i++) {
            String id = IDUtils.getId();
            SysJob sysJob = new SysJob();
            sysJob.setStatus(SysJobConstant.ING);
            sysJob.setType(SysHandlerEnum.SMSSendHandler.getCode());
            sysJob.setTarget(id);
            sysJob.setContext(id);
            sysJob.setCreateTime(new Date());
            sysJobList.add(sysJob);
        }
        sysJobService.saveBatch(sysJobList);
        return sysJobList.size();
    }
}
